package com.xilihui.xlh.business.activitys.store;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xilihui.xlh.R;
import com.xilihui.xlh.alipay.AuthResult;
import com.xilihui.xlh.alipay.PayResult;
import com.xilihui.xlh.business.activitys.ConfirmListingActivity;
import com.xilihui.xlh.business.activitys.MyChecklistActivity;
import com.xilihui.xlh.business.dialogs.DistributionDialog;
import com.xilihui.xlh.business.dialogs.InputDialog;
import com.xilihui.xlh.business.dialogs.PayDialog;
import com.xilihui.xlh.business.entities.ConfirmOrderEntity;
import com.xilihui.xlh.business.entities.PayEntity;
import com.xilihui.xlh.business.entities.StoreGetPay;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;
import com.xilihui.xlh.core.util.YSPUtils;

import org.feezu.liuli.timeselector.Utils.TextUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/12.
 */

public class ConfirmOrderActivity extends BaseCompatActivity implements OnRefreshListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.tv_total_jifeng)
    TextView tv_total_jifeng;
    @BindView(R.id.tv_totalPrice)
    TextView tv_total_price;
    
    ConfirmOrderEntity confirmOrderEntity;
    BaseAdapter<ConfirmOrderEntity.OrderBean.PStatusArrayBean> baseAdapter;
    ArrayList<ConfirmOrderEntity.OrderBean.PStatusArrayBean> data = new ArrayList<>();
    Float[] xiaoji;
    Integer[] shiyongjifen;
    String[] agent_ids;
    String[] beizhu;
    
    int jifen = 0;//可用积分
    int yjifen = 0;//已用积分
    int rate = 0;//比例
    
    float sum_jier = 0;//立即付款 总金额
    
    int h_jifen = 0;
    int h_yjifen = 0;

    int type = 1;
    String goods_id = "";
    String goods_num = "";
    String item_id = "";

    String jiner = "";//实付金额 支付成功显示
    String dyJiner = "";//抵用金额
    String order_all_id = "";//订单详情 id
    
    public static String orderInfo = "";
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    Intent intent;
    

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    Log.i("mylog","dddddd"+resultStatus);
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        ToastUtil.toastShortPositive("支付成功");
                        YEventBuses.post(new YEventBuses.Event("pay"));
                        Intent intent = new Intent(ConfirmOrderActivity.this, TradeSuccessActivity.class);
                        intent.putExtra("type",2);
                        intent.putExtra("jiner",jiner);
                        intent.putExtra("jifen",jifen);
                        startActivity(intent);
                        Log.i("mylog","支付成功");
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        ToastUtil.toastShortNegative("支付失败");
                        YEventBuses.post(new YEventBuses.Event("pay"));
                        Intent intent = new Intent(ConfirmOrderActivity.this, StoreOrderActivity.class);
                        startActivity(intent);
                    }
//                    intent = new Intent(ConfirmListingActivity.this,MyChecklistActivity.class);
//                    intent.putExtra("wa_id",wa_id);
//                    intent.putExtra("agent_id",agent_id);
//                    startActivity(intent);
//                    YEventBuses.post(new YEventBuses.Event("pay"));
//                    finish();
                    break;
                }
                case SDK_AUTH_FLAG: {
                    @SuppressWarnings("unchecked")
                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                    String resultStatus = authResult.getResultStatus();

                    // 判断resultStatus 为“9000”且result_code
                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
                        // 传入，则支付账户为该授权账户
                        ToastUtil.toastShortNegative("\"授权成功\\n\" + String.format(\"authCode:%s\", authResult.getAuthCode())");
                    } else {
                        // 其他状态值则为授权失败
                        ToastUtil.toastShortNegative("授权失败" + String.format("authCode:%s", authResult.getAuthCode()));
                    }
                    break;
                }
                default:
                    break;
            }
        };
    };
    
    @Override
    public int getLayout() {
        return R.layout.activity_confirm_order;
    }

    @Override
    public String initTitle() {
        return "确认订单";
    }

    @Override
    public void initUI(@Nullable final Bundle savedInstanceState) {
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setEnableLoadMore(false);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseAdapter = new BaseAdapter<ConfirmOrderEntity.OrderBean.PStatusArrayBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_confirm_order;
            }
            
            @Override
            protected void getView(final BaseViewHolder holder, final ConfirmOrderEntity.OrderBean.PStatusArrayBean entity, final int position) {
                if(position==0){
                    holder.setVisibility(R.id.constrantLayout, View.VISIBLE);
                    TextView tv_shopName = (TextView)holder.getView(R.id.tv_shopName);
                    TextView tv_phone = (TextView)holder.getView(R.id.tv_phone);
                    TextView tv_address = (TextView)holder.getView(R.id.tv_address);
                    
                    String address_id = confirmOrderEntity.getAddress().getAddress_id();
                    if(TextUtil.isEmpty(address_id)){
                        tv_shopName.setText("选择收货地址");
                        tv_address.setVisibility(View.GONE);
                        tv_phone.setText("");
                        holder.setOnClickListener(R.id.constrantLayout, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(ConfirmOrderActivity.this,AddressActivity.class);
                                startActivity(intent);
                            }
                        });
                    }else {
                        tv_shopName.setText(confirmOrderEntity.getAddress().getConsignee());
                        tv_phone.setText(confirmOrderEntity.getAddress().getMobile());
                        tv_phone.setCompoundDrawables(null,null,null,null);
                        tv_address.setText(confirmOrderEntity.getAddress().getAddress());
                        tv_address.setVisibility(View.VISIBLE);
                    }
                    
                    
                }else {
                    holder.setVisibility(R.id.constrantLayout,View.GONE);
                }
                
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.iv_good_img);
                simpleDraweeView.setImageURI(UrlConst.baseUrl() + entity.getOriginal_img());
                holder.setText(R.id.tv_name,entity.getGoods_name());
                holder.setText(R.id.tv_model,entity.getSpec_key_name());
                holder.setText(R.id.tv_jian_num,"共"+entity.getGoods_num()+"件商品，小计：");
                
                holder.setText(R.id.tv_place,"￥"+entity.getGoods_price());
                holder.setText(R.id.tv_totalNum,"x"+entity.getGoods_num());
                
                final float totalPrice = entity.getTotal_price();

                final TextView tv_pengsong = (TextView)holder.getView(R.id.tv_peisong);
                final TextView tv_hint_jifeng = (TextView)holder.getView(R.id.tv_hint_jifeng);
                final CheckBox checkBox = (CheckBox)holder.getView(R.id.cb_user_jifeng);
                final TextView tv_totalPrice = (TextView)holder.getView(R.id.tv_xiaoji);
                tv_totalPrice.setText("￥"+totalPrice);
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        
                            float jiner = jifen / rate;
                            if(b){
                                if(jifen>0){
                                    h_jifen = jifen;
                                    h_yjifen = yjifen;
                                    if(totalPrice>=jiner){
                                        float num = totalPrice - jiner;
                                        tv_totalPrice.setText("￥"+num);
                                        xiaoji[position] = num;
                                        shiyongjifen[position] = jifen;
                                        tv_hint_jifeng.setText("已用"+jifen+"积分抵用"+jiner+"元");
                                        yjifen = jifen;
                                        jifen = 0;
                                    }else {
                                        float num = jiner - totalPrice;
                                        int jf =  (int) (totalPrice * rate);
                                        if(jf==0){
                                            jf = 1;
                                        }
                                        jifen = jifen - jf;
                                        yjifen = jf;
                                        tv_totalPrice.setText("￥"+0);
                                        xiaoji[position] = (float)0;
                                        shiyongjifen[position] = jf;
                                        tv_hint_jifeng.setText("已用"+jf+"积分抵用"+totalPrice+"元");
                                    }
                                    entity.setSelect(true);
                                    handle();
                                }
                                
                            }else {
                                if(entity.isSelect()){
                                    jifen = h_jifen;
                                    yjifen = h_yjifen;
                            
                                    tv_hint_jifeng.setText("");
                                    tv_totalPrice.setText("￥"+totalPrice);
                                    xiaoji[position] = totalPrice;
                                    shiyongjifen[position] = 0;
                                    handle();
                                    entity.setSelect(false);
                                }else {
                                        
                                }
                            }

                    }
                });
                
                holder.setOnClickListener(R.id.ll_peisong, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DistributionDialog dialog = new DistributionDialog(ConfirmOrderActivity.this,
                                (ArrayList<ConfirmOrderEntity.OrderBean.PStatusArrayBean.DeliveriesBean>) entity.getDeliveries());
                        dialog.setOnSelectItem(new DistributionDialog.select() {
                            @Override
                            public void select(String name,String agend_id,int pos) {
                                for (int i=0;i<entity.getDeliveries().size();i++){
                                    if(i==pos){
                                        entity.getDeliveries().get(i).setFlag(true);
                                    }else {
                                        entity.getDeliveries().get(i).setFlag(false);
                                    }
                                }
                                holder.setText(R.id.tv_peisong,name);
                                agent_ids[position] = agend_id;
                            }
                        });
                        dialog.show();
                    }
                });
                    
                holder.setOnClickListener(R.id.ll_beizhu, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        InputDialog inputDialog = new InputDialog(ConfirmOrderActivity.this);
                        inputDialog.setOnInputListener(new InputDialog.OnInputListener() {
                            @Override
                            public void beizhu(String content) {
                                beizhu[position] = content;
                                holder.setText(R.id.tv_beizhu,content);
                            }
                        });
                        inputDialog.show();
                    }
                });
                                
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        
    }
    
    /**
     * 处理显示总金额 积分
     */
    public void handle(){
        int myjifen = 0;
        for (int i=0;i<shiyongjifen.length;i++){
            myjifen = myjifen +shiyongjifen[i];
        }
        tv_total_jifeng.setText("可用"+jifen+"积分,"+"已用"+myjifen+"积分");
        float num = 0;
        for (int i=0;i<xiaoji.length;i++){
            num = num + xiaoji[i];
        }
        tv_total_price.setText("￥"+num);
        
    }

    @Override
    public void initData() {
        type = getIntent().getIntExtra("type",1);
        if(type==1){
            getData();
        }else if(type==2){
            goods_id = getIntent().getStringExtra("goods_id");
            goods_num = getIntent().getStringExtra("goods_num");
            item_id = getIntent().getStringExtra("item_id");
            lijibuy();
        }
    }
    
    
    public void getData(){
        
        StoreRequest.confirmOrder(this)
                .compose(DoTransform.<ConfirmOrderEntity>applyScheduler(this,true))
                .compose(this.<ConfirmOrderEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<ConfirmOrderEntity>(this) {
                    @Override
                    protected void onSuccess(ConfirmOrderEntity entity) {
                        data.clear();
                        data.addAll(entity.getOrder().getPStatusArray());
                        xiaoji = new Float[data.size()];
                        shiyongjifen = new Integer[data.size()];
                        for (int i=0;i<shiyongjifen.length;i++){
                            shiyongjifen[i] = 0;
                        }
                        agent_ids = new String[data.size()];
                        beizhu = new String[data.size()];
                        for (int i=0;i<beizhu.length;i++){
                            beizhu[i] = "";
                        }
                        
                        for (int i=0;i<data.size();i++){
                            xiaoji[i] = data.get(i).getTotal_price();
                        }
                        
                        confirmOrderEntity = entity;
                        baseAdapter.setList(data);
                        
                        
                        jifen = entity.getPay_points();
                        rate = entity.getPoint_rate();
                        sum_jier = entity.getOrder().getOrder_all_price();
                        
                        tv_total_jifeng.setText("可用"+jifen+"积分,"+"已用"+yjifen+"积分");
                        tv_total_price.setText("￥"+sum_jier);
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        ToastUtil.toastShortNegative(msg);
                    }
                    
                    
                });
        
    }

    public void lijibuy(){
        StoreRequest.lijibuy(this,goods_id,goods_num+"",item_id)
                .compose(DoTransform.<ConfirmOrderEntity>applyScheduler(this,true))
                .compose(this.<ConfirmOrderEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<ConfirmOrderEntity>(this) {
                    @Override
                    protected void onSuccess(ConfirmOrderEntity entity) {
                        data.clear();
                        data.addAll(entity.getOrder().getPStatusArray());
                        xiaoji = new Float[data.size()];
                        shiyongjifen = new Integer[data.size()];
                        for (int i=0;i<shiyongjifen.length;i++){
                            shiyongjifen[i] = 0;
                        }
                        agent_ids = new String[data.size()];
                        beizhu = new String[data.size()];
                        for (int i=0;i<beizhu.length;i++){
                            beizhu[i] = "";
                        }
                        
                        for (int i=0;i<data.size();i++){
                            xiaoji[i] = data.get(i).getTotal_price();
                        }

                        confirmOrderEntity = entity;
                        baseAdapter.setList(data);


                        jifen = entity.getPay_points();
                        rate = entity.getPoint_rate();
                        sum_jier = entity.getOrder().getOrder_all_price();

                        tv_total_jifeng.setText("可用"+jifen+"积分,"+"已用"+yjifen+"积分");
                        tv_total_price.setText("￥"+sum_jier);

                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getData();
    }
    
    @OnClick(R.id.tv_fukuang)
    public void fukuang(){

        for (int i = 0;i<agent_ids.length;i++){
            if(TextUtil.isEmpty(agent_ids[i])){
                ToastUtil.toastShortNegative("请选择配送方式");
                return;
            }
        }
        
        PayDialog payDialog = new PayDialog(ConfirmOrderActivity.this);
        payDialog.setOnItemSelectListener(new PayDialog.OnItemSelectListener() {
            @Override
            public void close() {

            }

            @Override
            public void alipay() {
                addOrder("alipay");
            }

            @Override
            public void weixin() {
                addOrder("weixin");
            }
              
        });
        
        payDialog.show();
        
        
    }
    
    public void addOrder(final String code){
        try {
            JSONArray jsonArray = new JSONArray();
            for (int i=0;i<data.size();i++) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", data.get(i).getId());
                jsonObject.put("goods_id",data.get(i).getGoods_id());
                jsonObject.put("goods_num",data.get(i).getGoods_num());
                jsonObject.put("spec_item_id",data.get(i).getSpec_item_id());
                jsonObject.put("user_note",beizhu[i]);
                jsonObject.put("integral",shiyongjifen[i]);
                jsonObject.put("agent_id",agent_ids[i]);
                jsonArray.put(i,jsonObject);
            }
                        
            StoreRequest.addOrder(ConfirmOrderActivity.this,confirmOrderEntity.getAddress().getAddress_id(),type+"",
                    jsonArray.toString())
                    .compose(DoTransform.<StoreGetPay>applyScheduler(this,true))
                    .compose(this.<StoreGetPay>bindToLifecycle())
                    .subscribe(new HttpSubscriber<StoreGetPay>(this) {
                        @Override
                        protected void onSuccess(final StoreGetPay entity) {
                            
                            
                            StoreRequest.pay(ConfirmOrderActivity.this,entity.getOrder_no(),code)
                                    .compose(DoTransform.<PayEntity>applyScheduler(ConfirmOrderActivity.this,true))
                                    .subscribe(new HttpSubscriber<PayEntity>(ConfirmOrderActivity.this) {
                                        @Override
                                        protected void onSuccess(PayEntity ent) {
                                            jiner = ent.getOrder_info().getOrder_price();
                                            dyJiner = ent.getOrder_info().getOrder_all_give_money();
                                            if(entity.getOrder_price()>0){
                                                if(code.equals("alipay")){
                                                    orderInfo = ent.getAli_payment_params();
                                                    payV2(tv_total_jifeng);
                                                }else if(code.equals("weixin")){
                                                    SPUtil.put(ConfirmOrderActivity.this,"jiner",jiner);
                                                    SPUtil.put(ConfirmOrderActivity.this,"jifen",dyJiner);
                                                    SPUtil.put(ConfirmOrderActivity.this,"order_id",order_all_id);
                                                    weixiPay(ent);
                                                }
                                            }else {
                                                Intent intent = new Intent(ConfirmOrderActivity.this, TradeSuccessActivity.class);
                                                intent.putExtra("jiner",jiner);
                                                intent.putExtra("jifen",dyJiner);
                                                intent.putExtra("type",2);
                                                startActivity(intent);
                                                YEventBuses.post(new YEventBuses.Event("pay"));
                                            }
                                            
                                            
                                        }

                                        @Override
                                        protected void onFail(String msg) {
                                            ToastUtil.toastShortNegative(msg);
                                        }
                                    });
                            
                        }

                        @Override
                        protected void onFail(String msg) {
                            ToastUtil.toastShortNegative(msg);
                        }
                    });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    

    public void weixiPay(PayEntity payEntity){

        LogUtil.i("mylog","weixi_pay"+payEntity.toString());
        
        boolean isTiaozhuang = true;//是否是 确认清单的跳转
        SPUtil.put(ConfirmOrderActivity.this,SPUtil.PAY_TYPE,2);
        SPUtil.put(ConfirmOrderActivity.this,SPUtil.STORE_TZ,true);
        
        IWXAPI msgApi = WXAPIFactory.createWXAPI(ConfirmOrderActivity.this,"wx086831d3499ea9c2", true);
        msgApi.registerApp("wx086831d3499ea9c2");
        
        boolean isWXAppInstalled = msgApi .isWXAppInstalled();
        if(isWXAppInstalled){
            PayReq request = new PayReq();
            request.appId = payEntity.getWeixin_payment_params().getAppid();
            request.partnerId = payEntity.getWeixin_payment_params().getPartnerid();
            request.prepayId= payEntity.getWeixin_payment_params().getPrepayid();
            request.packageValue = "Sign=WXPay";
            request.nonceStr= payEntity.getWeixin_payment_params().getNoncestr();
            request.timeStamp= payEntity.getWeixin_payment_params().getTimestamp()+"";
            request.sign= payEntity.getWeixin_payment_params().getSign();
            msgApi.sendReq(request);
        }else {
            ToastUtil.toastShortPositive("请先安装微信");
        }
            
    }

    /**
     * 支付宝支付业务
     *
     * @param v
     */
    public void payV2(View v) {
//        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);


//        /**
//         * 这里只是为了方便直接向商户展示支付宝的整个支付流程；所以Demo中加签过程直接放在客户端完成；
//         * 真实App里，privateKey等数据严禁放在客户端，加签过程务必要放在服务端完成；
//         * 防止商户私密数据泄露，造成不必要的资金损失，及面临各种安全风险； 
//         *
//         * orderInfo的获取必须来自服务端；
//         */
//        boolean rsa2 = true;
//        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);
//          
//        
//
//        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
//        final String orderInfo = orderParam + "&" + sign;

        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(ConfirmOrderActivity.this);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();

    }

    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is("add_address")){
            tv_total_jifeng.postDelayed(new Runnable() {
                @Override
                public void run() {
                    getData();
                }
            },1000);
        }else if(event.is("pay")){
            finish();
        }
    }
    
    
}
